package com.rain.shoppingscienceproj.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.rain.shoppingscienceproj.R;
import com.rain.shoppingscienceproj.module.Goods;
import com.rain.shoppingscienceproj.utils.ImageUtils;

import java.util.List;

/**
 * Author: Rain
 * Create: 2018/9/1 18:08
 */
public class StoreAdapter extends BaseAdapter {
    private Context mContext;
    private List<Goods> list;

    public StoreAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setList(List<Goods> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        } else {
            return list.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        MyViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new MyViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_store_layout, null);
            viewHolder.tv_title = convertView.findViewById(R.id.tv_title);
            viewHolder.iv_image = convertView.findViewById(R.id.iv_image);
            viewHolder.tv_money = convertView.findViewById(R.id.tv_money);
            viewHolder.tv_person_num = convertView.findViewById(R.id.tv_person_num);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (MyViewHolder) convertView.getTag();
        }
        Goods goods = list.get(position);
        if (null != goods.getGoodsImage() || !"".equals(goods.getGoodsImage())) {
            ImageUtils.image(mContext, goods.getGoodsImage(), viewHolder.iv_image);
        }
        viewHolder.tv_title.setText(goods.getGoodsName());
        viewHolder.tv_person_num.setText(goods.getGoodsNum() + "付款");
        viewHolder.tv_money.setText(Double.valueOf(goods.getGoodsPay()) / 100 + "");
        return convertView;
    }

    class MyViewHolder {
        ImageView iv_image;
        TextView tv_title, tv_money, tv_person_num;
    }
}
